package main

import (
	"fmt"
	"hi-jvm/classfile"
	"hi-jvm/classpath"
	hiCmd "hi-jvm/cmd"
	"hi-jvm/main/submain"
	"strings"
)
func main() {
	cmd := hiCmd.ParseCmd()
	if cmd.VersionFlag {
		fmt.Println("version 0.0.1")
	} else if cmd.HelpFlag || cmd.Class == "" {
		hiCmd.PrintUsage()
	} else {
		startJVM(cmd)
	}
}

func startJVM(cmd * hiCmd.Cmd) {
	cmdStartJVM(cmd)
}

func cmdStartJVM(cmd * hiCmd.Cmd) {
	fmt.Printf("classpath:%s class:%s args:%v\n", cmd.CpOption, cmd.Class, cmd.Args)
}

func classpathStartJVM(cmd * hiCmd.Cmd) {
	cp := classpath.Parse(cmd.XjreOption, cmd.CpOption)
	fmt.Printf("classpath:%v class:%v args:%v\n",
		cp, cmd.Class, cmd.Args)

	className := strings.Replace(cmd.Class, ".", "/", -1)
	classData, _, err := cp.ReadClass(className)
	if err != nil {
		fmt.Printf("Could not find or load main class %s \n", cmd.Class)
	}
	fmt.Printf("class data:%\n", classData)
}

func rtdaStartJVM(cmd * hiCmd.Cmd) {
	//frame := rtda.NewFrame(nil, 100, 100)
	//submain.TestLocalVars(frame.LocalVars)
	//submain.TestOperandStack(frame.OperandStack)
}

func classfileStartJVM(cmd * hiCmd.Cmd) {
	submain.StartJVM_classfile(cmd)
}

func getMainMethod(cf *classfile.ClassFile) *classfile.MemberInfo {
	for _, m := range cf.Methods {
		if m.Name() == "main" && m.Descriptor() == "([Ljava/lang/String;)V" {
			return m
		}
	}
	return nil
}